/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.siro.pm.PmApplication;
import com.siro.pm.utils.DatabaseOperation;
import com.siro.pm.utils.DatabasePool;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since Developer.java v1.0
 */
public class Developer implements Serializable {
	private static final long serialVersionUID = 5508368657552165002L;
	private static final Logger logger = LoggerFactory
			.getLogger(Developer.class);

	// id, 姓名，密码，角色，职务名称，移动电话，邮件地址，所属部门或组织
	private String id;
	private String name;
	private String password;
	private String role;
	private String postName;
	private String phoneNum;
	private String mailAddr;
	private String orgName;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getPostName() {
		return postName;
	}

	public void setPostName(String postName) {
		this.postName = postName;
	}

	public String getPhoneNum() {
		return phoneNum;
	}

	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}

	public String getMailAddr() {
		return mailAddr;
	}

	public void setMailAddr(String mailAddr) {
		this.mailAddr = mailAddr;
	}

	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	public static boolean isProjectManager(Developer manager) {
		return ((Developer) PmApplication.getDefault().getUser()).getId()
				.equals(manager.getId());
	}

	public static void insertDeveloper(Developer developer) {
		logger.info("添加开发者，id:{},name:{}", developer.getId(),
				developer.getName());
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO siropm_developer VALUES('");
		sql.append(developer.getId()).append("','");
		sql.append(developer.getName()).append("','");
		sql.append(developer.getPassword()).append("','");
		sql.append(developer.getRole()).append("','");
		sql.append(developer.getPostName()).append("','");
		sql.append(developer.getPhoneNum()).append("','");
		sql.append(developer.getMailAddr()).append("','");
		sql.append(developer.getOrgName()).append("')");
		try {
			DatabaseOperation.excuteUpdateSQL(DatabasePool.getConnection(),
					sql.toString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		logger.info("添加开发者，id:{},name:{} 成功", developer.getId(),
				developer.getName());
	}

	public static void deleteDeveloper(Developer developer) {
		logger.info("删除开发者，id:{},name:{}", developer.getId(),
				developer.getName());
		String sql = "DELETE FROM siropm_developer WHERE id='"
				+ developer.getId() + "'";
		try {
			DatabaseOperation
					.excuteUpdateSQL(DatabasePool.getConnection(), sql);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		logger.info("删除开发者，id:{},name:{} 成功", developer.getId(),
				developer.getName());
	}

	public static void updateDeveloper(Developer developer) {
		logger.info("更新开发者，id:{} name:{}", developer.getId(),
				developer.getName());
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE siropm_developer SET ");
		sql.append("name='").append(developer.getName()).append("', ");
		sql.append("password='").append(developer.getPassword()).append("', ");
		sql.append("role='").append(developer.getRole()).append("',");
		sql.append("postName='").append(developer.getPostName()).append("',");
		sql.append("phoneNum='").append(developer.getPhoneNum()).append("',");
		sql.append("mailAddr='").append(developer.getMailAddr()).append("',");
		sql.append("orgName='").append(developer.getOrgName()).append("'");
		sql.append("WHERE id='").append(developer.getId()).append("'");
		try {
			DatabaseOperation.excuteUpdateSQL(DatabasePool.getConnection(),
					sql.toString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		logger.info("更新开发者，id:{} name:{} 成功", developer.getId(),
				developer.getName());
	}

	public static List<Developer> getAllDevelopers() {
		logger.info("获取所有开发者...");
		List<Developer> developers = new ArrayList<Developer>();
		String sql = "SELECT id,name,password,role,postName,phoneNum,mailAddr,orgName FROM siropm_developer";
		try {
			Map<Integer, List<String>> rslt = DatabaseOperation
					.excuteSelectSQL(DatabasePool.getConnection(), sql);
			List<String> row = null;
			for (int i = 1; i <= rslt.size(); i++) {
				row = rslt.get(i);
				Developer developer = new Developer();
				developer.setId(row.get(0));
				developer.setName(row.get(1));
				developer.setPassword(row.get(2));
				if (row.get(3) != null && !row.get(3).equals(""))
					developer.setRole(row.get(3));
				if (row.get(4) != null && !row.get(4).equals(""))
					developer.setPostName(row.get(4));
				if (row.get(5) != null && !row.get(5).equals(""))
					developer.setPhoneNum(row.get(5));
				if (row.get(6) != null && !row.get(6).equals(""))
					developer.setMailAddr(row.get(6));
				if (row.get(7) != null && !row.get(7).equals(""))
					developer.setOrgName(row.get(7));
				developers.add(developer);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return developers;
	}

	public static Developer getDeveloper(String id) {
		logger.info("获取开发者,ID:" + id);
		Developer developer = null;
		String sql = "SELECT id,name,password,role,postName,phoneNum,mailAddr,orgName FROM siropm_developer WHERE id='"
				+ id + "'";
		try {
			Map<Integer, List<String>> rslt = DatabaseOperation
					.excuteSelectSQL(DatabasePool.getConnection(), sql);
			if (rslt == null || rslt.size() == 0)
				return null;
			List<String> row = rslt.get(1);
			developer = new Developer();
			developer.setId(row.get(0));
			developer.setName(row.get(1));
			developer.setPassword(row.get(2));
			if (row.get(3) != null && !row.get(3).equals(""))
				developer.setRole(row.get(3));
			if (row.get(4) != null && !row.get(4).equals(""))
				developer.setPostName(row.get(4));
			if (row.get(5) != null && !row.get(5).equals(""))
				developer.setPhoneNum(row.get(5));
			if (row.get(6) != null && !row.get(6).equals(""))
				developer.setMailAddr(row.get(6));
			if (row.get(7) != null && !row.get(7).equals(""))
				developer.setOrgName(row.get(7));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return developer;
	}

	public static List<Developer> getDevelopers(Project project) {
		logger.info("获取工程：{}的所有开发者...", project.getName());
		List<Developer> developers = new ArrayList<Developer>();
		String sql = "SELECT projectid,developerid FROM siropm_project_developer WHERE projectid='"
				+ project.getId() + "'";
		try {
			Map<Integer, List<String>> rslt = DatabaseOperation
					.excuteSelectSQL(DatabasePool.getConnection(), sql);
			List<String> row = null;
			for (int i = 1; i <= rslt.size(); i++) {
				row = rslt.get(i);
				Developer developer = Developer.getDeveloper(row.get(1));
				developers.add(developer);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return developers;
	}
}
